//
//  SISSMyEnrollPassengerViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/2.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMyEnrollPassengerViewController.h"
#import "SISSNetworkingManager+Mine.h"
#import "SISSPassengerResourceModel.h"
#import "SISSPassengerCell.h"
#import "SISSEnrollPassengerDetailViewController.h"

static NSString *myEnrollPassengerCellReuseID = @"myEnrollPassengerCellReuseID";
@interface SISSMyEnrollPassengerViewController ()

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/** 房源数组 */
@property (strong, nonatomic) NSArray<SISSPassengerResourceModel *> *passengerResourceArr;



@end

@implementation SISSMyEnrollPassengerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestMyEnrollPassengerResourceData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.passengerResourceArr.count];
    return self.passengerResourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSPassengerCell *cell = [tableView dequeueReusableCellWithIdentifier:myEnrollPassengerCellReuseID];
    cell.isHideButton = YES;
    cell.passengerModel = self.passengerResourceArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSEnrollPassengerDetailViewController *vc = [[SISSEnrollPassengerDetailViewController alloc] init];
    vc.title = @"客源详情";
    vc.isChange = YES;
    vc.passengerModel = self.passengerResourceArr[indexPath.row];
    self.passengerResourceArr[indexPath.row].read = 1;
    [self.tableView reloadData];
    vc.deleteComplete = ^{
        
        [self requestMyEnrollPassengerResourceData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}




#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSPassengerCell" bundle:nil] forCellReuseIdentifier:myEnrollPassengerCellReuseID];
}




#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    
    [self requestMyEnrollPassengerResourceData];
}

- (void)reloadMoreData {
    
    [self requestMyEnrollPassengerResourceData];
}
#pragma mark - 加载数据
- (void)requestMyEnrollPassengerResourceData {
    
    
    [kNetworkManager requestOfflinePassengerWithsuccess:^(NSArray<SISSPassengerResourceModel *> *passengerResources) {
        
        self.passengerResourceArr = passengerResources;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}






@end
